package com.alander.nexusgo.user.service.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.alander.nexusgo.user.model.entity.UserPreferences;
import com.alander.nexusgo.user.service.UserPreferencesService;
import com.alander.nexusgo.user.mapper.UserPreferencesMapper;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
* @author alander
*/
@Service
public class UserPreferencesServiceImpl extends ServiceImpl<UserPreferencesMapper, UserPreferences>
    implements UserPreferencesService {

    @Override
    public UserPreferences getUserPreferences(Long userId) {
        LambdaQueryWrapper<UserPreferences> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPreferences::getUserId, userId)
                   .eq(UserPreferences::getStatus, "0");

        UserPreferences preferences = this.getOne(queryWrapper);
        if (preferences == null) {
            // 如果用户偏好设置不存在，则初始化
            preferences = initUserPreferences(userId);
        }

        return preferences;
    }

    @Override
    public boolean updateUserPreferences(Long userId, UserPreferences preferences) {
        LambdaQueryWrapper<UserPreferences> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPreferences::getUserId, userId);

        UserPreferences existingPreferences = this.getOne(queryWrapper);
        if (existingPreferences != null) {
            // 更新现有偏好设置
            preferences.setId(existingPreferences.getId());
            return this.updateById(preferences);
        } else {
            // 创建新的偏好设置
            preferences.setUserId(userId);
            preferences.setStatus("0");
            return this.save(preferences);
        }
    }

    @Override
    public UserPreferences initUserPreferences(Long userId) {
        LambdaQueryWrapper<UserPreferences> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPreferences::getUserId, userId)
                .eq(UserPreferences::getStatus, "0");
        UserPreferences existingPreferences = this.getOne(queryWrapper);
        UserPreferences preferences = new UserPreferences();
        preferences.setUserId(userId);
        preferences.setStatus("0"); // 0表示正常状态

        preferences.setTravelStyle("leisure,cultural,foodie");
        preferences.setBudgetRange(BigDecimal.ZERO);
        preferences.setAccommodationType("hotel,bnb");
        preferences.setTransportPreference("train,car");
        preferences.setFoodPreference("local,international");
        preferences.setInterestTags("[]");
        preferences.setPreferredCities("[]");
        preferences.setAvoidCities("[]");
        preferences.setTravelCompanions("solo");
        preferences.setLanguagePreference("zh-CN");
        preferences.setTimezone("Asia/Shanghai");

        // 创建隐私设置对象
        JSONObject privacySettings = new JSONObject();
        privacySettings.putOpt("profile_visibility", "public");
        privacySettings.putOpt("checkin_visibility", "friends_only");
        privacySettings.putOpt("allow_stranger_messages", false);
        preferences.setPrivacySettings(privacySettings.toString());

        // 创建通知设置对象
        JSONObject notificationSettings = new JSONObject();
        notificationSettings.putOpt("msg_notify", true);
        notificationSettings.putOpt("like_notify", true);
        notificationSettings.putOpt("follow_notify", true);
        preferences.setNotificationSettings(notificationSettings.toString());
        if (existingPreferences != null) {
            this.updateById( preferences);
        }else{
            this.save( preferences);
        }
        return preferences;
    }
}
